package com.resolution.dietaryassessment.view.pagoda;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.resolution.dietaryassessment.BaseActivity;
import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.listener.MyListener;
import com.resolution.dietaryassessment.view.main.MainActivity;
import com.resolution.dietaryassessment.view.pagoda.fragment.TabFragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;

/**
 *  @author By Resolusion
 *  @Title: DietaryPagodaActivity
 *  膳食宝塔
 *  @Date: 2020/1/8
 *  @Time: 13:36
 *  @email res01ution@foxmail.com
 */
public class DietaryPagodaActivity extends BaseActivity {

    private List<String> titles;
    private int[] img = new int[]{R.drawable.baota06baby,R.drawable.baota612baby,
            R.drawable.baota13child,R.drawable.baotapreschooler,R.drawable.baotaprepreg,
            R.drawable.baotaearlypreg,R.drawable.baotapreg,R.drawable.baotalactatingwomen,
            R.drawable.baotaadult};

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dietary_pagoda);
        initView();
        initTabLayout();
    }

    private void initView() {
        Button my_dietary = findViewById(R.id.my_dietary);
        Button tcm_test = findViewById(R.id.tcm_constitution);
        Button remain = findViewById(R.id.re_main);
        Button leave = findViewById(R.id.leave_btn);

        my_dietary.setOnClickListener(new MyListener(this));
        tcm_test.setOnClickListener(new MyListener(this));
        leave.setOnClickListener(new MyListener(this));

        remain.setOnClickListener(v -> start(MainActivity.class));
    }

    private void initTabLayout() {

        titles = new ArrayList<>();
        Collections.addAll(titles,"0-6个月婴儿","6-12个月婴儿","1-3幼儿","学龄前儿童",
                "孕前期","孕早期","孕中、晚期","哺乳期妇女","成人");

        VerticalTabLayout tabLayout = findViewById(R.id.tablayout);
        List<Fragment> fragments = getFragments();
        tabLayout.setupWithFragment(getSupportFragmentManager(), R.id.container, fragments,
                new TabAdapter() {
                    @Override
                    public int getCount() {
                        return titles.size();
                    }

                    @Override
                    public ITabView.TabBadge getBadge(int position) {
                        return null;
                    }

                    @Override
                    public ITabView.TabIcon getIcon(int position) {
                        return null;
                    }

                    @Override
                    public ITabView.TabTitle getTitle(int position) {
                        return new QTabView.TabTitle.Builder()
                                .setContent(titles.get(position))
                                .setTextColor(0xff71c671, 0xffffffff)
                                .build();
                    }
                    @Override
                    public int getBackground(int position) {
                        return -1;
                    }
                });
        tabLayout.setTabSelected(0);
    }

    private List<Fragment> getFragments() {
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0;i < titles.size();i++){
            fragments.add(TabFragment.getInstance(img[i]));
        }
        return fragments;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}
